#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate scontrol update command for partitions.
############################################################################
# Copyright (C) 2002 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set authorized     1
set exit_code      0
set job_runnable   0
set part_name      ""
set part_old_state ""
set part_new_state ""
set read_state     ""
set timeout        $max_job_delay

# Execute a srun job in the specified partition name
# Returns 0 on successful completion, returns 1 otherwise
proc run_job { part_name } {
	global bin_printenv srun timeout
	set rc 1

	set srun_pid [spawn $srun --output=none --error=none -p $part_name -N1-128 -t1 $bin_printenv SLURMD_NODENAME]
	expect {
		-re "Unable to contact" {
			log_error "Slurm appears to be down"
			return 1
		}
		-re "uid" {
			set rc 0
			exp_continue
		}
		-re "not available" {
			slow_kill $srun_pid
			exp_continue
		}
		timeout {
			log_error "srun not responding"
			slow_kill $srun_pid
			return 1
		}
		eof {
			wait
		}
	}

	return $rc
}

#
# Identify a partition and its state
#
spawn $scontrol show part
expect {
	-re "PartitionName=($re_word_str)" {
		if {[string compare $part_name ""] == 0} {
			set part_name $expect_out(1,string)
		}
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}

spawn $scontrol show part $part_name
expect {
	-re "State=($re_word_str) " {
		set part_old_state $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}

#
# Validate current partition information
#
if {[string compare $part_name ""] == 0} {
	fail "scontrol unable to identify any partition"
}
if {[string compare $part_old_state ""] == 0} {
	fail "scontrol unable to identify state of partition $part_name"
}
if {[string compare $part_old_state "UP"] == 0} {
	set job_runable 1
	set part_new_state "DOWN"
}
if {[string compare $part_old_state "DOWN"] == 0} {
	set job_runable 0
	set part_new_state "UP"
}
if {[string compare $part_new_state ""] == 0} {
	fail "State of partition $part_name is invalid: $part_old_state"
}

#
# Confirm that a DOWN partition does not run any jobs
#
if {$job_runable == 0} {
	if {[run_job $part_name] == 0} {
		log_error "Job ran in DOWN partition"
		set exit_code 1
	}
}

#
# Change that partition's state
#
spawn $scontrol update PartitionName=$part_name State=$part_new_state
expect {
	-re "slurm_update error: ($re_word_str) ($re_word_str)" {
		set access_err 0
		set err_msg1 $expect_out(1,string)
		set err_msg2 $expect_out(2,string)
		if {[string compare $err_msg1 "Invalid"] == 0} {
			set access_err 1
		}
		if {[string compare $err_msg2 "user"] == 0} {
			set access_err 1
		}
		if {$access_err == 1} {
			skip "User not authorized to change slurm state"
		} else {
			set authorized 0
		}
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}

#
# Validate partition's new state
#
spawn $scontrol show partition $part_name
expect {
	-re "State=($re_word_str) " {
		set read_state $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
if {$authorized == 1} {
	if {[string compare $read_state $part_new_state] != 0} {
		log_error "scontrol state change error"
		set exit_code 1
	}
}

#
# Confirm that a DOWN partition does not run any jobs
#
if {$job_runable == 1} {
	if {[run_job $part_name] == 0} {
		log_error "Job ran in DOWN partition"
		set exit_code 1
	}
}

#
# Return that partition's state to its old value
#
spawn $scontrol update PartitionName=$part_name State=$part_old_state
expect {
	-re "slurm_update error: Invalid user id" {
		exp_continue
	}
	-re "slurm_update error:" {
		log_error "scontrol update error"
		set exit_code 1
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}

#
# Record that partition's state
#
spawn  $scontrol show partition $part_name
expect {

	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
